import { Component, instantiate, Node, Prefab, resources, _decorator } from 'cc';
import { GameOverPanel, GameOverPanelEventType } from '../../prefabs/ui/GameOverPanel';
import { StartPanelEvent, StartPanelEventType } from '../../prefabs/ui/start-panel';
import { GamePlay, GamePlayEventType, PlayerData } from './GamePlay';

const { ccclass, property } = _decorator;

@ccclass('GameScene')
export default class GameScene extends Component {
	@property({ type: Node, tooltip: 'UI容器' })
	uiContaienr: Node = null!;

	@property(Node)
	gameplayNd: Node = null;

	private startPanel: Node;
	private gameOverPanel: Node;

	start() {
		this.showStartPanel();
	}

	private async showStartPanel() {
		if (!this.startPanel) {
			const prefab = await this.loadPrefab('prefabs/ui/StartPanel');
			if (!prefab) {
				return;
			}

			// 从预制体实例化出新的节点对象
			const node = instantiate(prefab);
			this.startPanel = node;
		}

		// 添加开始面板到显示列表
		// 方法1：this.startPanel.parent = this.uiContaienr;
		// 方法1：this.uiContaienr.addChild(this.startPanel);
		this.startPanel.parent = this.uiContaienr;

		// 监听事件, on：一直监听；once：只监听一次
		this.startPanel.once(StartPanelEventType.StartGame, this.onStartGame, this);
		this.node.on(StartPanelEventType.StartGame2, this.onStartGame2, this);
	}

	private removeStartPanel(destroy?: boolean) {
		if (this.startPanel) {
			// 取消某个事件的监听
			// this.startPanel.off(StartPanelEventType.StartGame, this.onStartGame, this);

			// 取消目标对象的所有事件的监听
			this.startPanel.targetOff(this);

			// 从父节点中移除
			this.startPanel.removeFromParent();

			// 如果此面板不在使用，则调用`destroy`进行销毁
			if (destroy) {
				this.startPanel.destroy();
			}
		}
	}

	private onStartGame() {
		this.removeStartPanel();
		this.removeGameOverPanel();

		this.gameplayNd.active = true;
		this.gameplayNd.on(GamePlayEventType.GameOver, this.onGameOver, this);

		// 获取节点上的挂载的组件
		const gamePlayComp: GamePlay = this.gameplayNd.getComponent(GamePlay);
		if (gamePlayComp) {
			gamePlayComp.startGame();
		}
	}

	private onStartGame2(event: StartPanelEvent) {
		console.log('receive event--->');
		console.log(event.type, event.detail);
	}

	private onGameOver(data: PlayerData) {
		this.gameplayNd.off(GamePlayEventType.GameOver, this.onGameOver, this);
		this.showGameOverPanel(data);
	}

	private async showGameOverPanel(data: PlayerData) {
		if (!this.gameOverPanel) {
			const prefab = await this.loadPrefab('prefabs/ui/GameOverPanel');
			if (!prefab) {
				return;
			}

			const node = instantiate(prefab);
			this.gameOverPanel = node;
		}

		const comp = this.gameOverPanel.getComponent(GameOverPanel);
		if (comp) {
			comp.setData(data);
		}

		this.gameOverPanel.parent = this.uiContaienr;
		this.gameOverPanel.on(GameOverPanelEventType.StartGame, this.onStartGame, this);
		this.gameOverPanel.on(GameOverPanelEventType.ShowMenu, this.onShowMenu, this);
	}

	private removeGameOverPanel(destroy?: boolean) {
		if (this.gameOverPanel) {
			this.gameOverPanel.targetOff(this);
			this.gameOverPanel.removeFromParent();
			if (destroy) {
				this.gameOverPanel.destroy();
				this.gameOverPanel = null;
			}
		}
	}

	private onShowMenu() {
		this.showStartPanel();
		this.removeGameOverPanel(true);
	}

	// 动态加载预制体，resources目录下的资源，直接使用`resources.load`进行加载
	private loadPrefab(url: string): Promise<Prefab | null> {
		return new Promise((resolve) => {
			resources.load(url, Prefab, (err, prefab) => {
				if (err) {
					resolve(null);
				}

				resolve(prefab);
			});
		});
	}
}
